var ctx;
var phoneReg = /^(13[0-9]|15[0-9]|18[0-9]|14[578])[0-9]{8}$/;

function setCtx(ctx) {
	this.ctx = ctx;
}

// 验证公司名称
function checkCompanyName() {
	var companyName = $("#companyName").val();
	//解决ie9以下placeholder与value重复的问题
	if(companyName == $("#companyName").attr("placeholder")){
		companyName = "";
	}
	
	if (companyName == null || companyName == "") {
		layer.tips('公司名称不能为空!', '#companyName', {
			tips : 3,
			time:1000
		});
		return false;
	} else {
		return true;
	}
}

//验证电话号码
function checkPhone(){
	var phone = $("#phone").val();
	//解决ie9以下placeholder与value重复的问题
	if(phone == $("#phone").attr("placeholder")){
		phone = "";
	}
	
	if(phone == null || phone == ""){
		layer.tips('电话号码不能为空!', '#phone', {
		    tips: 3,
			time:1000
		});
		return false;
	}else{
		if(!phoneReg.test(phone)){
			layer.tips('电话号码格式错误，请填写正确格式的电话号码!', '#phone', {
			    tips: 3,
				time:1000
			});
			return false;
		}else{
			return true;
		}
	}
}

//验证排序不能为空
function checkSort(){
	var reg = /^[0-9]*[1-9][0-9]*$/;
	var sort = $("#sort").val();
	if(sort == null || sort == ""){
		layer.tips('排序不能为空!', '#sort', {
			tips: 3,
			time:1000
		});
		return false;
	}else if(!reg.test(sort)){
		layer.tips('请输入正整数!', '#sort', {
			tips: 3,
			time:1000
		});
		return false;
	}else{
		return true;
	}
}

// 保存或新增方法
function saveCompany() {
	var companyName = $('#companyName').val();
	var companyAddr = $('#companyAddr').val();
	var phone = $('#phone').val();
	var sort = $('#sort').val();
	if ($('#companyId').val() != "" && $('#companyId').val() != undefined) {
		companyId = $('#companyId').val();
	} else {
		companyId = 0;
	}
	var imageLogoUrl = $('#imageLogo').val();
	var container = ue.getContent();
	
	//解决ie9以下placeholder与value重复的问题
	if(companyAddr == $("#companyAddr").attr("placeholder")){
		companyAddr = "";
	}

	if (checkCompanyName() && checkPhone() && checkSort()) {
		var index1 = layer.load(1);
		$.ajax({
			type : "POST",
			url : ctx + "/propertycompany/saveOrUpdateCompany",
			cache : false,
			dataType : 'json',
			data : {
				companyName : companyName,
				companyAddr : companyAddr,
				phone : phone,
				sort : sort,
				container : container,
				companyId : companyId,
				logo : imageLogoUrl
			},
			success : function(result) {
				if (result.success) {
					layer.msg("操作成功", {
						icon : 1,
						time : 3000
					// 2秒关闭（如果不配置，默认是3秒）
					}, function() {
						loadContent("/propertycompany/companyList");
					});
				} else {
					layer.close(index1);
					layer.msg("操作失败", {
						icon : 2
					});
				}
			}
		});
	}
}

//返回上一页
function cancel(){
	loadContent("/propertycompany/companyList");
}
